﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class InfoItem : ICreator { }




	/// <summary>
	/// InfoItems 类。
	/// </summary>
	public static partial class InfoItems
	{
		public const string INFO_TYPE = "InfoItem";



		#region GetItem
		/// <summary>
		/// 根据标识获取指定的信息项。
		/// </summary>
		/// <param name="o">信息项集合。</param>
		/// <param name="id">获取的信息项。</param>
		/// <returns>获取的信息项。</returns>
		public static InfoItem GetItem(this System.Data.Linq.Table<InfoItem> o, Guid id)
		{
			return o.SingleOrDefault(p => p.GlobalId == id);
		}

		public static InfoItem GetItem(Guid id)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.InfoItems.GetItem(id);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的信息项是否已经存在。
		/// </summary>
		/// <param name="o">信息项集合。</param>
		/// <param name="id">验证的信息项标识。</param>
		/// <returns>布尔值表示指定的信息项是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<InfoItem> o, Guid id)
		{
			return o.Any(p => p.GlobalId == id);
		}
		#endregion
	}



	/// <summary>
	/// InfoItems 类操作部分。
	/// </summary>
	public static partial class InfoItems
	{
		#region Register
		/// <summary>
		/// 注册信息项。
		/// </summary>
		/// <param name="info">注册的信息项。</param>
		/// <returns>注册的信息项。</returns>
		internal static InfoItem Register(InfoItem registerInfo)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoItem info = dc.InfoItems.GetItem(registerInfo.GlobalId);
			if (info == null)
			{
				info = registerInfo.ToNewEntity<InfoItem>();
				info.CreatorId = Users.CurrentUserId;
				info.CreationDate = DateTime.Now;
				dc.InfoItems.InsertOnSubmit(info);
				dc.SubmitChanges();
			}
			else
			{
				if (info.InfoType != registerInfo.InfoType)
					throw new ObjectExistsException(Res.InfoItems_RegisterInfoTypeNotAccord);
			}

			// 返回只读数据。
			return registerInfo.FillByEntity(info);
		}
		#endregion



		#region SetPersistent
		/// <summary>
		/// 设置信息项的持久化状态。
		/// </summary>
		/// <param name="id">设置的信息项的标识。</param>
		/// <param name="isPersistent">是否已持久化。</param>
		internal static void SetPersistent(GenericModuleDataContext dc, Guid id, bool isPersistent)
		{
			InfoItem item = dc.InfoItems.GetItem(id);
			if (item == null) return;

			item.IsPersistent = isPersistent;

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}
		}

		/// <summary>
		/// 设置信息项的持久化状态。
		/// </summary>
		/// <param name="id">设置的信息项的标识。</param>
		/// <param name="isPersistent">是否已持久化。</param>
		internal static void SetPersistent(Guid id, bool isPersistent)
		{
			SetPersistent(new GenericModuleDataContext(), id, isPersistent);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除指定的信息项。
		/// </summary>
		/// <param name="id">删除的信息项标识。</param>
		public static void Delete(Guid id)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			InfoItem infoItem = dc.InfoItems.GetItem(id);
			if (infoItem == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(dc, infoItem, true);	// 权限验证

			Delete(dc, infoItem);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Delete, INFO_TYPE, infoItem.GlobalId.ToString(), PermissionCode.Delete.ToString(), infoItem.InfoType);
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, InfoItem infoItem)
		{
			InfoResources.DeleteByParent(dc, infoItem);	// 删除关联数据
			dc.InfoItems.DeleteOnSubmit(infoItem);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// InfoItems 类对应操作权限部分。
	/// </summary>
	public static partial class InfoItems
	{
		#region GetDeleteRight
		/// <summary>
		/// 获取当前用户是否有删除信息项的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="infoItem">删除的信息项。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除信息项的权限。</returns>
		public static bool GetDeleteRight(GenericModuleDataContext dc, InfoItem infoItem, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Delete;
			bool b = dc.AccessController.GetUserIsHasPermission<InfoItem>(userName, infoItem, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, infoItem, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有删除信息项的权限。
		/// </summary>
		/// <param name="infoItem">删除的信息项。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除信息项的权限。</returns>
		public static bool GetDeleteRight(InfoItem infoItem, bool triggerException)
		{
			return GetDeleteRight(new GenericModuleDataContext(), infoItem, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有删除信息项的权限。
		/// </summary>
		/// <param name="infoItem">删除的信息项。</param>
		/// <returns>布尔值表示当前用户是否有删除信息项的权限。</returns>
		public static bool GetDeleteRight(InfoItem infoItem)
		{
			return GetDeleteRight(infoItem, false);
		}
		#endregion
	}



	
	/// <summary>
	/// InfoItems 类后台管理部分。
	/// </summary>
	public static partial class InfoItems
	{
		public static bool GetManageListRight()
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, InfoItems.ResourceId, (int)InfoItems.PermissionCode.Delete);
		}



		public static PaginationInfo<List<InfoItem>> GetPaginationManageList(string infoType, bool? isPersistent, string keyword, int pageSize, int currentPage)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;

			var q = from p in dc.InfoItems
					select p;

			if (!string.IsNullOrEmpty(infoType))
				q = q.Where(p => p.InfoType == infoType);
			if (isPersistent.HasValue)
				q = q.Where(p => p.IsPersistent == isPersistent.Value);
			if (!string.IsNullOrEmpty(keyword))
			{
				Guid globalId = new Guid(keyword);
				q = q.Where(p => p.GlobalId == globalId);
			}

			PaginationInfo<List<InfoItem>> pageInfo = new PaginationInfo<List<InfoItem>>();
			pageInfo.PageSize = pageSize;
			pageInfo.CurrentPage = currentPage;
			pageInfo.RecordCount = q.Count();
			pageInfo.Records = q.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
			return pageInfo;
		}
	}
}